package com.my.xq.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.my.xq.domain.OptionQueryParam;
import com.my.xq.domain.entity.Option;
import com.my.xq.domain.entity.Plan;
import com.my.xq.repository.OptionRepository;
import com.my.xq.repository.PlanRepository;
import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.sys.repository.MediaRepository;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/wjxx")
public class OptionServiceImpl {
	@Autowired
	PlanRepository planRepository;
	
	@Autowired
	OptionRepository optionRepository;
	
	@Autowired
	MediaRepository mediaRepository;
	
	/**
	 * @param c
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/addwjxxInfo", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "添加问卷选项信息")
	public void addwjxxInfo(Option c) {
		//成功添加问卷调查选项以后需要把对应方案的iscreateOption设置为true
		if(null==c.getId()||"".equals(c.getId())) {
			Plan p=planRepository.get(c.getPlanid());
			p.setId(c.getPlanid());
			p.setIscreateOption(true);
			planRepository.save(p);
		}else {
			Option o=optionRepository.get(c.getId());
			c.setAyes(o.getAyes());
			c.setNays(o.getNays());
		}
		optionRepository.save(c);
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/findAllPage", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询问卷选项信息并分页")
	@ApiOperation(value = "查询问卷选项信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "姓名", paramType = "form", dataType = "string"),
		@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
		@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public Page<Option> findAllPage(@ApiIgnore OptionQueryParam cqp,@ApiIgnore Pageable pageable,HttpServletRequest request) {
		Page<Option> page = optionRepository.findPage(cqp,pageable);
		return page;
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "根据投票方案查询所有的问卷选项", log = false)
	@RequestMapping(value ="/findOptionByPlan",method = { RequestMethod.GET })
	@ApiImplicitParams({ @ApiImplicitParam(name = "courtId", value = "小区id", paramType = "form", dataType = "string"),
		@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
		@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public Page<Option> findDoorplateByxq(OptionQueryParam oqp,
			@ApiIgnore Pageable pageable,HttpServletRequest request) {
		Page<Option> page = optionRepository.findPage(oqp,pageable);
		return page;
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "查询所有问卷选项信息", log = false)
	@RequestMapping("/findAll")
	public List<Option> findAll() {
		List<Option> list=optionRepository.findAll();
		return list;
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "查询一条问卷选项记录", log = false)
	@RequestMapping("/findOne")
	public Option findOne(String id,HttpServletRequest request) {
		Option court = optionRepository.get(id);
		return court;
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "根据id删除对应的问卷调查选项", log = false)
	@RequestMapping("/delwjxx")
	public void delwjxx(String id) {
		String planId=optionRepository.get(id).getPlanid();
		optionRepository.delete(id);
		List<Option> list=optionRepository.findAllOptionByPlan(planId);
		if(list.isEmpty()) {	//将对应选项的方案iscreateOption设置为false
			Plan p=planRepository.get(planId);
			p.setIscreateOption(false);
			planRepository.save(p);
		}
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "根据投票方案查询所有的问卷选项不分页", log = false)
	@RequestMapping("/findAllOptionByPlan")
	public List<Option> findAllOptionByPlan(String planid) {
		return optionRepository.findAllOptionByPlan(planid);
	}
	
	public List<String> toList(String value, String separator) {
		List<String> result = new ArrayList<String>();
		if (!this.isEmpty(value)) {
			String[] as = value.split(separator);
			for (int i = 0; i < as.length; i++) {
				result.add(as[i]);
			}
		}
		return result;
	}
	
    public boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}
